package com.flag.liutao.modules.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.role.domain.UserRole;
import com.flag.liutao.modules.role.mapper.UserRoleMapper;
import com.flag.liutao.modules.role.param.UserRoleParam;
import com.flag.liutao.modules.role.service.IUserRoleService;
import com.flag.liutao.modules.role.param.UserRoleQueryParam;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.role.vo.UserRoleVo;
import com.flag.liutao.modules.user.mapper.UserMapper;
import com.flag.liutao.modules.user.vo.UserQueryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-02
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> getUserRolePage(UserRoleQueryParam param) {
        IPage iPage = new Page(param.getPage(),param.getLimit());
        IPage<UserRoleVo> ipage = userRoleMapper.getUserRolePage(iPage,param.getGradeId(), param.getRoleId(),param.getUnitId(),param.getKeyword());
        Map<String,Object> map = new HashMap<>();
        map.put("content",iPage.getRecords());
        map.put("total",ipage.getTotal());
        return map;
    }

    @Override
    public void addUserRole(UserRoleParam param) {
        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getUserId,param.getUserId()).eq(UserRole::getGradeId,param.getGradeId()).eq(UserRole::getUnitId,param.getUnitId());
        UserRole userRoleDB = userRoleMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(userRoleDB)){
            userRoleDB.setRoleId(param.getRoleId());
            userRoleDB.setTakeTime(new Date());
            userRoleDB.setRetireTime(param.getRetireTime());
            userRoleMapper.updateById(userRoleDB);
        }else {
            UserRole userRole = new UserRole();
            BeanUtil.copyProperties(param,userRole);
            userRole.setTakeTime(new Date());
            userRoleMapper.insert(userRole);
        }

    }

    @Override
    public void editUserRole(UserRoleParam param) {
        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getUserId,param.getUserId()).eq(UserRole::getGradeId,param.getGradeId()).eq(UserRole::getUnitId,param.getUnitId());
        UserRole userRoleDB = userRoleMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(userRoleDB)){
            userRoleDB.setRoleId(param.getRoleId());
            userRoleDB.setTakeTime(param.getTakeTime());
            userRoleDB.setRetireTime(param.getRetireTime());
            userRoleMapper.updateById(userRoleDB);
        }
    }

    @Override
    public void delUserRole(UserRoleParam param) {
        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getUserId,param.getUserId()).eq(UserRole::getGradeId,param.getGradeId()).eq(UserRole::getUnitId,param.getUnitId()).eq(UserRole::getRoleId,param.getRoleId());
        UserRole userRole = userRoleMapper.selectOne(lqw);
        if(ObjectUtil.isNull(userRole)){
            throw new MyException("该任职信息不存在");
        } else {
            userRoleMapper.deleteById(userRole.getId());
            userMapper.deleteById(param.getUserId());
        }
    }
}
